setInterval(function(){flash();},500);

$(document).ready(function() {
    $('#nr').on('click', function(event) {
        var user_state = $("#user_state").val();
        var flag = 1;
        var e = this;
        if(user_state == "3"){
            flag = 0;
            layer.confirm('确定离开正在使用的资源吗?', {
                btn: ['取消','确定'],
                btnAlign: 'c', // 按钮居中显示
                btn1: function(){
                    layer.closeAll();
                },
                btn2: function(){
                    move(e,event)
                }
            })
        }
        if(flag == 1){
            move(e,event);
        }
    });
});

function move(e,event){
    var rect = $(e).offset(); // 获取父div的偏移位置
    var x = event.pageX - rect.left; // 计算鼠标在父div内的X坐标
    var y = event.pageY - rect.top; // 计算鼠标在父div内的Y坐标
    if(x<0||y<0||x>800||y>500){
        alert('移动点位位于地图外。')
    }else{
        $.ajax({
            type:"post",//请求类型
            url:"/module/world/playerMove",//请求地址
            dataType:"json",//交互的数据类型
            cache:false,//去除请求的缓存
            data:{
                toLeft:x,
                toTop:y
            },
            success:function(ret){
                if(ret.retCode=="0000"){
                }else{
                    alert(ret.retDesc);
                }
            }
        });
    }
}


function standup(){
    $.ajax({
        type:"post",//请求类型
        url:"/module/world/standUp",//请求地址
        dataType:"json",//交互的数据类型
        cache:false,//去除请求的缓存
        success:function(ret){
            if(ret.retCode=="0000"){
            }else{
                alert(ret.retDesc);
            }
        }
    });
}

function upset(id){
    event.stopPropagation();
    if(pd()){
        return;
    }

    var top = $("#"+id).attr("top");
    var left = $("#"+id).attr("left");
    var user_id = $("#user_id").val();
    var p_top = $("#"+user_id).attr("top");
    var p_left = $("#"+user_id).attr("left");
    var jl = Math.abs(Math.sqrt(Math.pow(p_top - top - 70, 2) + Math.pow(p_left - left - 35, 2)));
    if(jl > 80){
        alert("距离太远")
    }else{
        $.ajax({
            type:"post",//请求类型
            url:"/module/world/upset",//请求地址
            dataType:"json",//交互的数据类型
            cache:false,//去除请求的缓存
            data:{
                res_name:id
            },
            success:function(ret){
                if(ret.retCode=="0000"){
                }else{
                    alert(ret.retDesc);
                }
            }
        });
    }
}

function pd(){
    var user_state = $("#user_state").val();
    if(user_state == "3"){
        alert("先离开当前资源");
        return true;
    }

    if(user_state == "4"){
        alert("正在制造物品");
        return true;
    }
}

function flash(){
    $.ajax({
        type:"post",//请求类型
        url:"/module/world/getres",//请求地址
        dataType:"json",//交互的数据类型
        cache:false,//去除请求的缓存
        success:function(ret){
            $("#nr").html("");
            $.each(ret.retList, function(index, res) {
                var html = "";
                html += "<div class='zy-div' id='"+res.res_Name+"' xl='"+res.res_Hp+"' top='"+res.res_Top+"' left='"+res.res_Left+"' style='top:"+res.res_Top+"px;left:"+res.res_Left+"px;'>";
                if(res.res_Type == 'tree'){
                    html += "<div class='shuye'></div>";
                    html += "<div class='shu'></div>";
                }else if(res.res_Type == 'rock'){
                    html +="<div class='shi1'></div>";
                    html +="<div class='shi2'></div>";
                    html +="<div class='shi3'></div>";
                }
                html +="<div class='tx_div' onclick=upset('"+res.res_Name+"')>";
                if(res.useUser == null){
                    html +="<img class='tx'>";
                }else{
                    html +="<img class='tx' src="+res.useUser.user_img.file_path+res.useUser.user_img.file_name+" >";
                }
                html += "</div></div>";
                $("#nr").append(html);
                if(res.useUser != null){
                    var shakeDiv = document.getElementById(res.res_Name);
                    shakeDiv.style.animation = 'shake 0.1s linear';
                    setTimeout(function() {
                        shakeDiv.style.animation = ''; // 停止动画
                    }, 500); // 500ms 动画持续时间
                }
            });
            $.each(ret.objList1, function(index, res) {
                if(res.player_state != 3){
                    var html = "";
                    html +="<div class='user_div' id='"+res.player_info.user_id+"' top='"+(res.player_position.inTop-12)+"' left='"+(res.player_position.inLeft-12)+"' style='top:"+(res.player_position.inTop-12)+"px;left:"+(res.player_position.inLeft-12)+"px;'>";
                    html +="<img class='tx' src="+res.player_info.user_img.file_path+res.player_info.user_img.file_name+" >";
                    html += "</div>";
                    $("#nr").append(html);
                    if(res.player_position != null && (res.player_position.inTop != res.player_position.toTop && res.player_position.inLeft != res.player_position.toLeft)){
                        $("#"+res.player_info.user_id).animate({
                            top: res.player_position.inTop+res.player_position.goTop-12+'px',
                            left: res.player_position.inLeft+res.player_position.goLeft-12+'px'
                        }, 100);
                    }
                }
                var user_id = $("#user_id").val();
                if(res.player_info.user_id==user_id){
                    $("#user_state").val(res.player_state);
                }
            });
            $("#bbq").html("");
            $.each(ret.objList, function(index, res) {
                var html = "";
                html += "<li>";
                html += res.name+":"+res.count;
                html += "</li>";
                $("#bbq").append(html);
            });
            $.each(ret.objList2, function(index, res) {
                var html = "";
                html += "<li>";
                html += "<span class='"+res.prefix.level+"'>"+res.prefix.name+"</span>"+res.name;
                html += "<span>("+res.hp+"/"+res.maxHp+")</span>";
                if(res.state=="1"){
                    html += "<input type='button' class='zb' value='装备' data_name="+res.name+" data_code="+res.code+" onClick='zb(this)' >";
                }
                if(res.state=="2"){
                    html += "<input type='button' class='qxzb' value='取消装备' data_name="+res.name+" data_code="+res.code+" onClick='qxzb(this)' >";
                }
                if(res.hp < res.maxHp){
                    html += "<input type='button' class='xfzb' value='修复装备' data_name="+res.name+" data_hp="+res.hp+" data_maxHp="+res.maxHp+" data_code="+res.code+" onClick='xfzb(this)' >";
                }
                html += "<input type='button' class='dq' value='丢弃' data_name="+res.name+" data_code="+res.code+" onClick='dq(this)' >";
                html += "</li>";
                $("#bbq").append(html);
            });
        }
    });
}


function zz(button) {
    if(pd()){
        return;
    }
    var id = button.getAttribute('data_id');
    var name = button.getAttribute('data_name');
    var time = button.getAttribute('data_needtime');

    var count = $("#zzc"+id).val();
    if(count == null || count <= 0){
        alert("输入制造数量");
        return;
    }
    var t = time*count;
    layer.confirm('确定制造'+count+'个'+name+'?预计需要'+t+"个系统秒.", {
        btn: ['取消','确定'],
        btnAlign: 'c', // 按钮居中显示
        btn1: function(){
            layer.closeAll();
        },
        btn2: function(){
            $.ajax({
                type:"post",//请求类型
                url:"/module/world/createItem",//请求地址
                dataType:"json",//交互的数据类型
                cache:false,//去除请求的缓存
                data:{
                    id:id,
                    count:count
                },
                success:function(ret){
                    if(ret.retCode=="0000"){
                    }else{
                        alert(ret.retDesc);
                    }
                }
            });
        }
    })
}
function dq(button) {
    if(pd()){
        return;
    }
    var code = button.getAttribute('data_code');
    var name = button.getAttribute('data_name');
    layer.confirm('确定取消装备'+name+'?', {
        btn: ['取消','确定'],
        btnAlign: 'c', // 按钮居中显示
        btn1: function(){
            layer.closeAll();
        },
        btn2: function(){
            $.ajax({
                type:"post",//请求类型
                url:"/module/world/deleteItem",//请求地址
                dataType:"json",//交互的数据类型
                cache:false,//去除请求的缓存
                data:{
                    code:code,
                    type:"1"
                },
                success:function(ret){
                    if(ret.retCode=="0000"){
                    }else{
                        alert(ret.retDesc);
                    }
                }
            });
        }
    })
}

function xfzb(button) {
    alert("还没做");
}

function qxzb(button) {
    if(pd()){
        return;
    }
    var code = button.getAttribute('data_code');
    var name = button.getAttribute('data_name');
    layer.confirm('确定取消装备'+name+'?', {
        btn: ['取消','确定'],
        btnAlign: 'c', // 按钮居中显示
        btn1: function(){
            layer.closeAll();
        },
        btn2: function(){
            $.ajax({
                type:"post",//请求类型
                url:"/module/world/removeItem",//请求地址
                dataType:"json",//交互的数据类型
                cache:false,//去除请求的缓存
                data:{
                    code:code,
                    type:"1"
                },
                success:function(ret){
                    if(ret.retCode=="0000"){
                    }else{
                        alert(ret.retDesc);
                    }
                }
            });
        }
    })
}
function zb(button){
    if(pd()){
        return;
    }
    var code = button.getAttribute('data_code');
    var name = button.getAttribute('data_name');
    layer.confirm('确定装备'+name+'?', {
        btn: ['取消','确定'],
        btnAlign: 'c', // 按钮居中显示
        btn1: function(){
            layer.closeAll();
        },
        btn2: function(){
            $.ajax({
                type:"post",//请求类型
                url:"/module/world/useItem",//请求地址
                dataType:"json",//交互的数据类型
                cache:false,//去除请求的缓存
                data:{
                    code:code,
                    type:"1"
                },
                success:function(ret){
                    if(ret.retCode=="0000"){
                    }else{
                        alert(ret.retDesc);
                    }
                }
            });
        }
    })
}

